package org.bridge.util.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.xml.sax.InputSource;

public class XmlUtil {

	public static void mashall(Mapping mapping,Object obj,Writer writer) throws Exception{
		Marshaller marshaller = new Marshaller();
		if(writer!=null){
			marshaller.setWriter(writer);
		}
			
		marshaller.setMapping(mapping);
		marshaller.marshal(obj);
	}
	
	public static Object unmarshall(Mapping mapping,InputStream xml,Class _class) throws Exception{
		Unmarshaller unmarshaller = null;
		if(_class!=null){
			unmarshaller = new Unmarshaller(_class);
		}
		else{
			unmarshaller = new Unmarshaller();
		}
		
		unmarshaller.setMapping(mapping);
		Object result = unmarshaller.unmarshal(new InputSource(xml));
		return result;
	}
	
}
